package leetcode.N301_N400;

import java.util.HashSet;
import java.util.Set;

public class N349 {

    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<>(nums1.length);
        for (int i : nums1) {
            set.add(i);
        }

        Set<Integer> result = new HashSet<>(Math.min(nums2.length, nums1.length));

        for (int i : nums2) {
            if (set.contains(i)) {
                result.add(i);
            }
        }

        int[] r = new int[result.size()];

        int index = 0;
        for (int i : result) {
            r[index++] = i;
        }
        return r;
    }


}
